import QtQuick 2.0

Item {
    id: root
    property int handleWidth: 10        //可拖拽的宽度
    property color color: "#017ff6"     //主色 边框和指示点的颜色
    property bool debug: false

    Rectangle{
        id: _selectRect
        anchors.fill: parent
        anchors.margins: handleWidth / 2
        color: "transparent"
        border.color: root.color
        border.width: 2
        MouseArea{
            anchors.fill: parent
            anchors.margins: handleWidth
            drag.target: root
            cursorShape: Qt.SizeAllCursor
        }
    }
    Component{
        id: _mouseArea
        MouseArea {
            hoverEnabled: true
            property int lastX: 0
            property int lastY: 0
            signal moved(int moveX, int moveY)
            cursorShape: Qt.SizeVerCursor
            onPressedChanged: {
                if (pressed) {
                    lastX = mouseX;
                    lastY = mouseY;
                }
            }
            onPositionChanged: {
                if (pressed) {
                    moved(mouseX - lastX, mouseY - lastY)
                }
            }
            Rectangle{
                anchors.centerIn: parent
                width: root.handleWidth
                height: root.handleWidth
                color: root.color
                border.color: "white"
                border.width: 1
            }
            //调试用 鼠标区域的边框
            Rectangle{
                id: _rectDebug
                anchors.fill: parent
                color: "transparent"
                border.color: "red"
                border.width: 1
                visible: root.debug
            }
        }
    }

    //上下左右
    Loader{
        id: _loaderTop
        anchors.top: root.top
        anchors.horizontalCenter: root.horizontalCenter
        sourceComponent: _mouseArea
        width: root.width - handleWidth*2
        height: handleWidth
        Connections{
            target: _loaderTop.item
            onMoved:{
                if (root.height - moveY > root.handleWidth) {
                    root.y += moveY
                    root.height -= moveY
                } else {
                    root.height = root.handleWidth
                }
            }
        }
    }

    Loader{
        id: _loaderBottom
        anchors.bottom: root.bottom
        anchors.horizontalCenter: root.horizontalCenter
        sourceComponent: _mouseArea
        width: root.width - handleWidth*2
        height: handleWidth
        Connections{
            target: _loaderBottom.item
            onMoved:{
                if ( root.height + moveY > root.handleWidth) {
                    root.height += moveY
                } else {
                    root.height = root.handleWidth
                }
            }
        }
    }

    Loader{
        id: _loaderLeft
        anchors.left: root.left
        anchors.verticalCenter: root.verticalCenter
        sourceComponent: _mouseArea
        height: root.height - handleWidth*2
        width: handleWidth
        onLoaded: item.cursorShape = Qt.SizeHorCursor
        Connections{
            target: _loaderLeft.item
            onMoved:{
                if (root.width - moveX > root.handleWidth) {
                    root.x += moveX
                    root.width -= moveX
                } else {
                    root.width = root.handleWidth
                }
            }
        }
    }

    Loader{
        id: _loaderRight
        anchors.right: root.right
        anchors.verticalCenter: root.verticalCenter
        sourceComponent: _mouseArea
        height: root.height - handleWidth*2
        width: handleWidth
        onLoaded: item.cursorShape = Qt.SizeHorCursor
        Connections{
            target: _loaderRight.item
            onMoved:{
                if (root.width + moveX > root.handleWidth) {
                    root.width += moveX
                } else {
                    root.width = root.handleWidth
                }
            }
        }
    }

    //左上、右上、左下、右下
    Loader{
        id: _loaderTopLeft
        anchors.top: root.top
        anchors.left: root.left
        sourceComponent: _mouseArea
        height: handleWidth
        width: handleWidth
        onLoaded: item.cursorShape = Qt.SizeFDiagCursor
        Connections{
            target: _loaderTopLeft.item
            onMoved:{
                if (root.height - moveY > root.handleWidth) {
                    root.y += moveY
                    root.height -= moveY
                } else {
                    root.height = root.handleWidth
                }
                if (root.width - moveX > root.handleWidth){
                    root.x += moveX
                    root.width -= moveX
                } else {
                    root.width = root.handleWidth
                }
            }
        }
    }
    Loader{
        id: _loaderTopRight
        anchors.top: root.top
        anchors.right: root.right
        sourceComponent: _mouseArea
        height: handleWidth
        width: handleWidth
        onLoaded: item.cursorShape = Qt.SizeBDiagCursor
        Connections{
            target: _loaderTopRight.item
            onMoved:{
                if (root.height - moveY > root.handleWidth) {
                    root.y += moveY
                    root.height -= moveY
                } else {
                    root.height = root.handleWidth
                }
                if (root.width + moveX > root.handleWidth) {
                    root.width += moveX
                } else {
                    root.width = root.handleWidth
                }
            }
        }
    }
    Loader{
        id: _loaderBottomLeft
        anchors.bottom: root.bottom
        anchors.left: root.left
        sourceComponent: _mouseArea
        height: handleWidth
        width: handleWidth
        onLoaded: item.cursorShape = Qt.SizeBDiagCursor
        Connections{
            target: _loaderBottomLeft.item
            onMoved:{
                if (root.height + moveY > root.handleWidth) {
                    root.height += moveY
                    root.x += moveX
                } else {
                    root.height = root.handleWidth
                }
                if (root.width - moveX > root.handleWidth) {
                    root.width -= moveX
                } else {
                    root.width = root.handleWidth
                }
            }
        }
    }
    Loader{
        id: _loaderBottomRight
        anchors.bottom: root.bottom
        anchors.right: root.right
        sourceComponent: _mouseArea
        height: handleWidth
        width: handleWidth
        onLoaded: item.cursorShape = Qt.SizeFDiagCursor
        Connections{
            target: _loaderBottomRight.item
            onMoved:{
                if (root.height + moveY > root.handleWidth) {
                    root.height += moveY
                } else {
                    root.height = root.handleWidth
                }
                if (root.width + moveX > root.handleWidth) {
                    root.width += moveX
                } else {
                    root.width = root.handleWidth
                }
            }
        }
    } 
}
